<template>
  <div class="home-container">
    <!-- 轮播图 -->
    <div class="banner">
      <el-carousel height="300px">
        <el-carousel-item>
          <div class="banner-item banner-1">
            <h2>欢迎使用校园失物招领平台</h2>
            <p>让每件失物都能找到回家的路</p>
          </div>
        </el-carousel-item>
        <el-carousel-item>
          <div class="banner-item banner-2">
            <h2>快速发布 便捷查询</h2>
            <p>简单几步，发布你的失物或拾获信息</p>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 统计信息 -->
    <div class="stats">
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="stat-card">
            <el-icon class="stat-icon" color="#409eff"><Document /></el-icon>
            <div class="stat-info">
              <div class="stat-value">{{ stats.totalItems }}</div>
              <div class="stat-label">总发布信息</div>
            </div>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="stat-card">
            <el-icon class="stat-icon" color="#67c23a"><CircleCheck /></el-icon>
            <div class="stat-info">
              <div class="stat-value">{{ stats.claimedItems }}</div>
              <div class="stat-label">已成功认领</div>
            </div>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="stat-card">
            <el-icon class="stat-icon" color="#e6a23c"><User /></el-icon>
            <div class="stat-info">
              <div class="stat-value">{{ stats.totalUsers }}</div>
              <div class="stat-label">注册用户</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 最新失物/招领 -->
    <div class="latest-items">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="最新失物" name="lost">
          <el-row :gutter="20">
            <el-col :span="6" v-for="item in lostItems" :key="item.id">
              <el-card shadow="hover" class="item-card" @click="viewDetail(item.id)">
                <div class="item-name">{{ item.itemName }}</div>
                <div class="item-info">
                  <el-tag size="small">{{ getCategoryName(item.category) }}</el-tag>
                  <span class="item-time">{{ formatDate(item.lostDate) }}</span>
                </div>
                <div class="item-location">
                  <el-icon><Location /></el-icon>
                  {{ item.locationArea }}
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="最新招领" name="found">
          <el-row :gutter="20">
            <el-col :span="6" v-for="item in foundItems" :key="item.id">
              <el-card shadow="hover" class="item-card" @click="viewDetail(item.id)">
                <div class="item-name">{{ item.itemName }}</div>
                <div class="item-info">
                  <el-tag size="small" type="success">{{ getCategoryName(item.category) }}</el-tag>
                  <span class="item-time">{{ formatDate(item.lostDate) }}</span>
                </div>
                <div class="item-location">
                  <el-icon><Location /></el-icon>
                  {{ item.locationArea }}
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getItemList } from '@/api/item'

const router = useRouter()
const activeTab = ref('lost')
const stats = ref({
  totalItems: 0,
  claimedItems: 0,
  totalUsers: 0
})
const lostItems = ref([])
const foundItems = ref([])

const categoryNames = ['电子设备', '证件卡类', '学习用品', '生活用品', '衣物鞋帽', '其他']

const getCategoryName = (category) => categoryNames[category] || '未知'

const formatDate = (date) => {
  return new Date(date).toLocaleDateString()
}

const viewDetail = (id) => {
  router.push(`/detail/${id}`)
}

const loadData = async () => {
  try {
    // 加载失物列表
    const lostRes = await getItemList({ type: 0, current: 1, size: 8 })
    lostItems.value = lostRes.data.records
    
    // 加载招领列表
    const foundRes = await getItemList({ type: 1, current: 1, size: 8 })
    foundItems.value = foundRes.data.records
    
    // 模拟统计数据
    stats.value = {
      totalItems: lostRes.data.total + foundRes.data.total,
      claimedItems: Math.floor((lostRes.data.total + foundRes.data.total) * 0.6),
      totalUsers: 1234
    }
  } catch (error) {
    console.error(error)
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
}

.banner {
  margin-bottom: 30px;
  border-radius: 8px;
  overflow: hidden;
}

.banner-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 300px;
  color: white;
  text-align: center;
}

.banner-1 {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.banner-2 {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.banner-item h2 {
  font-size: 32px;
  margin-bottom: 10px;
}

.banner-item p {
  font-size: 18px;
}

.stats {
  margin-bottom: 30px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 30px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.stat-icon {
  font-size: 48px;
  margin-right: 20px;
}

.stat-value {
  font-size: 32px;
  font-weight: bold;
  color: #303133;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}

.latest-items {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.item-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: transform 0.3s;
}

.item-card:hover {
  transform: translateY(-5px);
}

.item-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.item-time {
  font-size: 12px;
  color: #909399;
}

.item-location {
  font-size: 14px;
  color: #606266;
  display: flex;
  align-items: center;
}

.item-location .el-icon {
  margin-right: 5px;
}
</style>

